﻿using Lamex.Base;
using Lamex.Interface;
using Lamex.Library;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.mNC.UI
{
    public partial class Frm_NC_FeatData_FromCreo : frmBaseChild
    {
        public Frm_NC_FeatData_FromCreo()
        {
            InitializeComponent();
        }

        private void Frm_NC_FeatData_FromCreo_Load(object sender, EventArgs e)
        {
            try { this.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理
        }
        public override void DoView(IButtonInfo sender)
        {
            Thread fThread = new Thread(new ThreadStart(Function));//开辟一个新的线程
            fThread.Start();
        }
        public void Function()
        {
            //获取当前文件夹下所有的文件
            string path = @"\\Tx-fs10\tx-fs1\LAMEX\Parametric\FeatData\Aluminum";
            string pathBack = @"\\Tx-fs10\tx-fs1\LAMEX\NC\FeatData\Aluminum\";
            DirectoryInfo folder = new DirectoryInfo(path);
            int isums = 0, icount_true = 0, icount_false = 0;
            try
            {
                foreach (FileInfo file in folder.GetFiles("*.txt"))
                {
                    isums++;
                    DataTable TABLE_CREO = Common.GetTable.GetTable_FromCreo();
                    string fileName = file.FullName;
                    //采用流的方式读取
                    StreamReader sr = new StreamReader(fileName, Encoding.Default);
                    string nextLine;
                    while ((nextLine = sr.ReadLine()) != null)
                    {
                        //是否需要删除旧数据

                        DataRow dr = TABLE_CREO.NewRow();
                        string strList = nextLine.ToString();
                        string[] str = strList.Split(';');
                        dr["DWG_NO"] = str[0];
                        dr["MODULE_CODE"] = str[1];
                        dr["PART_NO"] = str[2];
                        dr["PART_LENGTH"] = str[3];
                        dr["WORK_SURFACE"] = str[4];
                        dr["FACE_WIDTH"] = str[5];
                        dr["FACE_HEIGHT"] = str[6];
                        dr["UDF_NAME"] = str[7];
                        dr["FEATURE_TYPE"] = str[8];
                        dr["DEEP_CODE"] = str[9];
                        dr["COORD_X"] = str[10];
                        dr["COORD_Y"] = str[11];
                        dr["COORD_Z"] = str[12];
                        dr["FEATURE_DIA"] = str[13].ToUpper() == "NULL" ? null : str[13];
                        dr["SLOT_LENGTH"] = str[14].ToUpper() == "NULL" ? null : str[14];
                        dr["SLOT_WIDTH"] = str[15].ToUpper() == "NULL" ? null : str[15];
                        dr["FILLIT_RADIUS"] = str[16].ToUpper() == "NULL" ? null : str[16];
                        dr["CREO_OUTPUT_TIME"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                        dr["UPDATE_TIME"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                        dr["UPDATE_USER"] = Lamex.Common.Loginer.CurrentUser.Account;
                        TABLE_CREO.Rows.Add(dr);
                    }
                    ////批量插入数据库
                    //if (BaseBuildSql.ExcuteInsert(TABLE_CREO, "NC_PN_AL_FeatData_Creo"))
                    //{
                    //    TABLE_CREO = new DataTable();
                    //    //导入成功后移动文件到新的文件夹
                    //    File.Copy(fileName, pathBack + file.Name, true);
                    //    sr.Close();
                    //    //sr.Dispose();//销毁对象
                    //    File.Delete(fileName);
                    //    icount_true++;
                    //}
                    //else
                    //{
                    //    icount_false++;
                    //}
                    SetTextMessage(isums * 100 / 14);//把14改成总数即可
                }
                DevExpress.XtraEditors.XtraMessageBox.Show("共【" + isums + "】个文件，刷新成功【" + icount_true + "】个，失败【" + icount_false + "】个！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message);
            }
           

        }
        private delegate void SetPos(int ipos);
        private void SetTextMessage(int ipos)
        {
            if (this.InvokeRequired)
            {
                SetPos setpos = new SetPos(SetTextMessage);
                this.Invoke(setpos, new object[] { ipos });
            }
            else
            {

                //this.frmProcessEllipseUI1.Value = Convert.ToInt32(ipos);
            }
        }
    }
}
